<?

class TexyPlugin {

	function __construct(cCMS $cms) 
	{
		$this->cms = $cms;

		if (!isset($this->cms->texy)) 
		{

			$texyPath = dirname(__FILE__).'/texy/';

			// include libs
			require_once ($texyPath . 'texy-compact.php');
			
			$this->cms->texy = &new Texy();
			$this->cms->texy->utf=true;

			// set user callback function for /-- code blocks
			$this->cms->texy->blockModule->codeHandler = array($this,"fshl");
			
			$this->cms->texy->smiliesModule->root = $this->cms->file->getRelative($this->cms->url,$this->cms->file->getPartDir("standard","text")."texy/texy/smilies/");
			$this->cms->texy->smiliesModule->allowed = true;
			$this->cms->texy->smiliesModule->icons = array (
                        ':)'  =>  'smile.gif',
                        ':('  =>  'sad.gif',
                        ';)'  =>  'wink.gif',
                        '.)'  =>  'wink.gif',
                        ':)))'  =>  'biggrin.gif',
                        ':D'  =>  'biggrin.gif',
                        '8O'  =>  'eek.gif',
                        '8O'  =>  'eek.gif',
                        '8)'  =>  'cool.gif',
                        ':?'  =>  'confused.gif',
                        ':x'  =>  'mad.gif',
                        ':P'  =>  'razz.gif',
                        ':|'  =>  'neutral.gif',
	            );

			// '<style type="text/css">'. file_get_contents($fshlPath.'styles/COHEN_style.css') . '</style>';
			//$text = '<style type="text/css">@import(\''. $this->cms->file->getRelative($this->cms->fromDir, $fshlPath.'styles/COHEN_style.css') . '\');</style>'.$text;

			// processing
			$this->cms->texy->headingModule->balancing = false; //no hX balancing
		}
		
	}

	function getShowText($fieldName, $id, $value, $view, $part)
	{
		if ($view->doctype!='raw') {
			$value = $this->cms->texy->process($value);  
		}
				
		return $value;
	}

	function fshl(&$element)
	{
		$fshlPath = dirname(__FILE__).'/fshl/';
		include_once ($fshlPath . 'fshl.php');	
		
	    $lang = strtoupper($element->lang);
    	if ($lang == 'JAVASCRIPT') $lang = 'JS';
	    if (!in_array(
    	        $lang,
        	    array('CPP', 'CSS', 'HTML', 'JAVA', 'PHP', 'JS', 'SQL'))
	       ) return;

		    $parser = new fshlParser($element->texy->utf ? 'HTML_UTF8' : 'HTML'); //| P_LINE_COUNTER
		    
		    $code = $parser->highlightString($lang,$element->content);
		    
		    // proc, to nevim
		    $code = str_replace("    ","  ",$code);
		    
		    $element->setContent( htmlspecialchars($code), TRUE);
		    $element->type = null;
		    $element->lang = null;
	}

}

?>